/*
 * @lc app=leetcode.cn id=1656 lang=javascript
 *
 * [1656] 设计有序流
 */

// @lc code=start
/**
 * @param {number} n
 */
var OrderedStream = function (n) {
  this.queue = Array(n + 1);
  this.ptr = 1;
};

/** 
 * @param {number} idKey 
 * @param {string} value
 * @return {string[]}
 */
OrderedStream.prototype.insert = function (idKey, value) {
  this.queue[idKey] = value;
  let result = [];
  if (idKey === this.ptr) {
    for (let i = idKey; i < this.queue.length; i++) {
      if (this.queue[i]) {
        result.push(this.queue[i]);
      } else {
        this.ptr = i;
        break;
      }
    }
  }
  return result;
};

/**
 * Your OrderedStream object will be instantiated and called as such:
 * var obj = new OrderedStream(n)
 * var param_1 = obj.insert(idKey,value)
 */
// @lc code=end
